<?php
class CustomerAction extends CommonAction{
	    public $pp;
	    function __construct(){	     	
			   if(!isset($_SESSION['Login'])&&($_SESSION['Login']!=true)){
		           $this->redirect('Login/login');
		       }
			   
			   $Uid = $_SESSION['UserId'];
			   $permissions = M("permissions");
			   
			   //获取到当前用户对OA管理模块的操作权限
			   $pp = $permissions->where("p_column_id = 5 and p_uid = {$Uid}")->find();
			   $p_column_sonName = $pp['p_column_sonName'];
			   $p_column_son = $pp['p_column_son'];  
			   
			   $p_column_son = explode(",",$p_column_son);
			   $p_column_sonName = explode("|",$p_column_sonName);
			   
			   $this->pp = $p_column_son;  
	   	}
		
      //管理客户页面
	  public function customer(){
		  $permissions_info = $this->pp;
			   if($permissions_info[1]!=1){
				  $this->error("对不起,您没有对本模块的操作权限"); 
		  }
		  
		  $Uid = $_SESSION['UserId'];
		  $this->Online($_SESSION['UserId']);
		  $customer = M("customer");
		  import('ORG.Util.page'); //导入分页类(非原TINKPHP分页类)
		  $count = $customer->where("c_uid = {$Uid}")->count();//获取总数
		  $page = new Page($count,10);
		  $c_info = $customer->where("c_uid = {$Uid}")->limit($page->limit)->select();
		  $this->assign("c_info",$c_info);
		  $this->assign("fpage",$page->fpage(1,4,5,6,0,3));
		  $this->display();  
	  }
	  //新建客户页面
	  public function customer_add(){
		  $permissions_info = $this->pp;
			   if($permissions_info[0]!=1){
				  $this->error("对不起,您没有对本模块的操作权限"); 
		  }
		  $this->display();  
	  }
	  
	  //新建客户处理页面
	  public function do_customer_add(){
		  $Uid = $_SESSION['UserId'];
		  $customer = M("customer");
		  $customerMore = M("customermore");
		  //获取oa_customer表的内容
		  $data['c_uid']       = $Uid;
		  $data['c_name']      = $_POST['c_name'];
		  $data['c_phone']     = $_POST['c_phone'];
		  $data['c_spots']     = $_POST['c_spots'];
		  $data['c_hots']      = $_POST['c_hots'];
		  $data['c_hots_info'] = $_POST['c_hots_info'];
		  $data['c_sex']       = $_POST['c_sex'];
		  $data['c_type']      = $_POST['c_type'];
		  $data['c_phase']     = $_POST['c_phase'];
		  $data['c_status']    = $_POST['c_status'];	
		  
		  //获取oa_customermore表的内容
		  $data2['m_telephone']   = $_POST['m_telephone'];
		  $data2['m_email']       = $_POST['m_email'];
		  $data2['m_fax']         = $_POST['m_fax'];
		  $data2['m_qq']          = $_POST['m_qq'];
		  $data2['m_zip']         = $_POST['m_zip'];
		  $data2['m_address']     = $_POST['m_address'];
		  $data2['m_company']     = $_POST['m_company'];
		  $data2['m_companyAddress']   = $_POST['m_companyAddress'];
		  $data2['m_business']    = $_POST['m_business'];
		  $data2['m_appellation'] = $_POST['m_appellation'];
		  $data2['m_position']    = $_POST['m_position'];
		  $data2['m_source']      = $_POST['m_source'];
		  $data2['m_credit']      = $_POST['m_credit'];
		  $data2['m_relation']    = $_POST['m_relation'];
		  $data2['m_jiazhi']      = $_POST['m_jiazhi'];
		  $data2['m_contact']     = $_POST['m_contact'];
		  $data2['m_papers_type'] = $_POST['m_papers_type'];
		  $data2['m_papers_num']  = $_POST['m_papers_num'];
		  $data2['m_note']        = nl2br($_POST['m_note']);
		  $data2['m_addtime']     = time();
		  
		  if($c_id = $customer->add($data)){
			  $data2['m_cid'] = $c_id;
			  if($customerMore->add($data2)){
				 $this->Online($Uid);
			     $this->Log($Uid,"添加客户",1);
				 $this->success("客户添加成功");  
			  }
		  }else{
			  $this->Online($Uid);
			  $this->Log($Uid,"添加客户",0);
			  $this->error("客户添加失败"); 
		  }
	  }
	  
	  //查看/修改客户详细信息
	  public function look_customerInfo(){
		  $permissions_info = $this->pp;
			   if($permissions_info[1]!=1){
				  $this->error("对不起,您没有对本模块的操作权限"); 
		  }
		  
	      $c_id = $_GET['c_id'];		  
		  //查询出客户的详细信息
		  $customer = M("customer");
		  $sql = "select * from oa_customer as c,oa_customermore as m where c.c_id = m.m_cid and c.c_id = {$c_id}";
		  $c_info = $customer->query($sql);
	      $this->assign("c_info",$c_info);
		  
		  $this->Online($_SESSION['UserId']);
		  $this->display();
	  }
	  
	  //用户信息更新操作
	  public function do_edit_customer(){
		  
		  $c_id = $_POST['c_id'];
		  
		  $customer = M("customer");
		  $customerMore = M("customermore");
		  //获取oa_customer表的更新资料
		  $data['c_name']      = $_POST['c_name'];
		  $data['c_phone']     = $_POST['c_phone'];
		  $data['c_spots']     = $_POST['c_spots'];
		  $data['c_hots']      = $_POST['c_hots'];
		  $data['c_hots_info'] = $_POST['c_hots_info'];
		  $data['c_sex']       = $_POST['c_sex'];
		  $data['c_type']      = $_POST['c_type'];
		  $data['c_phase']     = $_POST['c_phase'];
		  $data['c_status']    = $_POST['c_status'];
		  
		  //获取oa_customermore表的内容
		  $data2['m_telephone']   = $_POST['m_telephone'];
		  $data2['m_email']       = $_POST['m_email'];
		  $data2['m_fax']         = $_POST['m_fax'];
		  $data2['m_qq']          = $_POST['m_qq'];
		  $data2['m_zip']         = $_POST['m_zip'];
		  $data2['m_address']     = $_POST['m_address'];
		  $data2['m_company']     = $_POST['m_company'];
		  $data2['m_companyAddress']   = $_POST['m_companyAddress'];
		  $data2['m_business']    = $_POST['m_business'];
		  $data2['m_appellation'] = $_POST['m_appellation'];
		  $data2['m_position']    = $_POST['m_position'];
		  $data2['m_source']      = $_POST['m_source'];
		  $data2['m_credit']      = $_POST['m_credit'];
		  $data2['m_relation']    = $_POST['m_relation'];
		  $data2['m_jiazhi']      = $_POST['m_jiazhi'];
		  $data2['m_contact']     = $_POST['m_contact'];
		  $data2['m_papers_type'] = $_POST['m_papers_type'];
		  $data2['m_papers_num']  = $_POST['m_papers_num'];
		  $data2['m_note']        = nl2br($_POST['m_note']);
		  
		  if($customer->where("c_id = {$c_id}")->save($data)){
			 $ok_1 = "ok";  
		  }
		  if($customerMore->where("m_cid = {$c_id}")->save($data2)){
			 $ok_2 = "ok"; 
		  }
		  
		  if($ok_1=="ok"||$ok_2=="ok"){
			 $this->Online($Uid);
			 $this->Log($Uid,"更新客户资料",1);
			 $this->success("客户资料更新成功",U("Customer/customer"));  
		  }else{
			 $this->Online($Uid);
			 $this->Log($Uid,"更新客户资料",0);
			 $this->error("对不起,客户资料更新失败");  
		  }	    
	  } 
	  
	  //查询客户信息处理
	  public function customer_query(){
		  $permissions_info = $this->pp;
			   if($permissions_info[1]!=1){
				  $this->error("对不起,您没有对本模块的操作权限"); 
		  }
		  
		  $Uid = $_SESSION['UserId'];
		  $filed_name = $_GET['field_name']; 

		  $value  = $_GET['value'];
		  $new_filed = substr($filed_name,0,1);
		  
		  import('ORG.Util.page'); //导入分页类(非原TINKPHP分页类)
		  $customer = M("customer");
		  if($new_filed=="c"){	
		  	$sql = "select * from oa_customer as c,oa_customermore as m where c.c_id = m.m_cid and c.".$filed_name." = '{$value}' and c.c_uid = {$Uid} {$page->limit}"; 
		  }else{
			$sql = "select * from oa_customer as c,oa_customermore as m where c.c_id = m.m_cid and m.".$filed_name." = '{$value}' and c.c_uid = {$Uid} {$page->limit}";
		  }
		  
		  $c_info = $customer->query($sql);
		  $count = count($c_info);
		  $page = new Page($count,10);
		  $this->assign("fpage",$page->fpage(1,4,5,6,0,3));
		  $this->assign("c_info",$c_info);
		  
		  $this->Online($_SESSION['UserId']);
		  $this->display(); 
	  }
	  
	  //共享和转移客户
	  public function shared_transfer(){
		  $permissions_info = $this->pp;
			   if($permissions_info[2]!=1){
				  $this->error("对不起,您没有对本模块的操作权限"); 
		  }
		  
		  //获取本员工的客户信息
		  $Uid = $_SESSION['UserId'];
		  $customer = M("customer");
		  $c_info = $customer->where("c_uid = {$Uid}")->select();
		  $this->assign("c_info",$c_info);
		  
		  //获取公司员工信息
		  $employees = M("employees");
		  $e_info = $employees->where("u_id != {$Uid}")->select();
		  $this->assign("e_info",$e_info);
		  
		  $this->Online($_SESSION['UserId']);
		  $this->display();
	  }
	  
	  //客户共享和转移处理
	  public function do_customer_caozuo(){
		  $caozuo_type = $_POST['customer_caozuo'];
		  if(empty($caozuo_type)){
			 $this->error("对不起,请选择您要执行的操作!!!");  
		  }
		  //获取提交上来的客户和员工信息
		  $customer_name = $_POST['customer_name'];
		  $employees_name = $_POST['employees_name'];
		  
		  $customer_id = implode(",",$customer_name);
		  
		  $customer = M("customer");
		  $customerMore = M("customermore");
		  $sql = "select * from oa_customer as c,oa_customermore as m where c.c_id = m.m_cid and c.c_id in({$customer_id})";
		  //共享处理
		  if($caozuo_type == "1"){
			  //获取到选择的用户信息			 
		      $c_info = $customer->query($sql); 
			  
			  //循环将要共享的客户 给到要共享的员工中
			  for($i=0;$i<count($employees_name);$i++){
				  for($j=0;$j<count($c_info);$j++){
					  $new_employeesName = explode("|",$employees_name[$i]);
					  $data['c_uid']   = $new_employeesName[0];
					  $data['c_name']  = $c_info[$j]['c_name'];
					  $data['c_phone'] = $c_info[$j]['c_phone'];
					  $data['c_spots'] = $c_info[$j]['c_spots'];
					  $data['c_hots']  = $c_info[$j]['c_hots'];
					  $data['c_hots_info']  = $c_info[$j]['c_hots_info'];
					  $data['c_sex']   = $c_info[$j]['c_sex']; 
					  $data['c_type']  = $c_info[$j]['c_type'];
					  $data['c_phase'] = $c_info[$j]['c_phase'];
					  $data['c_status']= $c_info[$j]['c_status']; 
					  $sum = $customer->where("c_name = '{$data[c_name]}' and c_uid = {$new_employeesName[0]}")->count();
					  
					  if($sum>0){
						 $this->error("对不起,【{$new_employeesName[1]}】已拥有客户【{$data['c_name']}】");  
					  }
					  
					if($m_id = $customer->add($data)){
					$data2['m_cid']			 = $m_id;
					$data2['m_telephone']   = $c_info[$j]['m_telephone'];
		  		    $data2['m_email']       = $c_info[$j]['m_email'];
					$data2['m_fax']         = $c_info[$j]['m_fax'];
					$data2['m_qq']          = $c_info[$j]['m_qq'];
					$data2['m_zip']         = $c_info[$j]['m_zip'];
					$data2['m_address']     = $c_info[$j]['m_address'];
					$data2['m_company']     = $c_info[$j]['m_company'];
					$data2['m_companyAddress']= $c_info[$j]['m_companyAddress'];
					$data2['m_business']    = $c_info[$j]['m_business'];
					$data2['m_appellation'] = $c_info[$j]['m_appellation'];
					$data2['m_position']    = $c_info[$j]['m_position'];
					$data2['m_source']      = $c_info[$j]['m_source'];
					$data2['m_credit']      = $c_info[$j]['m_credit'];
					$data2['m_relation']    = $c_info[$j]['m_relation'];
					$data2['m_jiazhi']      = $c_info[$j]['m_jiazhi'];
					$data2['m_contact']     = $c_info[$j]['m_contact'];
					$data2['m_papers_type'] = $c_info[$j]['m_papers_type'];
					$data2['m_papers_num']  = $c_info[$j]['m_papers_num'];
					$data2['m_note']        = $c_info[$j]['m_note'];
					$data2['m_addtime']        = $c_info[$j]['m_addtime'];
						 
					   if($customerMore->add($data2)){
						   $status = 1;
					   }else{
						   $status = 0;   
					   }
					}
					
				  }		    
			  }			  
			  if($status==1){
				   $this->Online($Uid);
				   $this->Log($Uid,"共享客户",1);
				   $this->success("恭喜你,客户共享成功");
			  }else{
				   $this->Online($Uid);
			       $this->Log($Uid,"共享客户",1);
				   $this->error("对不起,客户共享失败!!!");
			  }
			  			  
		  }
	  }
	  
	  
}
?>